package pl.edu.agh.climatex.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

import pl.edu.agh.climatex.util.Messages;

public class MalfunctionStatusComposite extends Composite {

	private Label acStatusLabel;
	private Label heaterStatusLabel;
	private Label fanStatusLabel;
	private Label tmStatusLabel;
	private List<Label> probes = new ArrayList<Label>();
	private Integer probesCount = 0;

	public MalfunctionStatusComposite(Composite parent, int style) {
		super(parent, style);
		this.setLayout(GridLayoutFactory.fillDefaults().create());
		initGUI();
	}

	private void initGUI() {
		Group group = new Group(this, SWT.NONE);
		group.setLayout(GridLayoutFactory.swtDefaults().numColumns(4).create());
		group.setLayoutData(GridDataFactory.fillDefaults().grab(true, true)
				.create());
		group.setText(Messages.MalfunctionStatusGroup);

		createACStatusLabel(group);
		createTMStatusLabel(group);
		createHeaterStatusLabel(group);
		createFanStatusLabel(group);

		this.getShell().pack();
	}

	private void createTMStatusLabel(Group group) {
		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(2)
				.create());
		composite.setLayoutData(GridDataFactory.fillDefaults().indent(10, 0)
				.align(SWT.END, SWT.FILL).span(2, 3).create());

		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(GridDataFactory.swtDefaults().create());
		label.setText(Messages.TMName);

		tmStatusLabel = new Label(composite, SWT.NONE);
		tmStatusLabel.setLayoutData(GridDataFactory.swtDefaults().create());
		setLabelAsOk(tmStatusLabel);

		Group probesGroup = new Group(composite, SWT.NONE);
		probesGroup.setLayout(GridLayoutFactory.swtDefaults().numColumns(2)
				.create());
		probesGroup.setLayoutData(GridDataFactory.fillDefaults().span(2, 1)
				.create());
		probesGroup.setText(Messages.ProbesGroup);

		for (int i = 0; i < 5; ++i) {
			addProbe(probesGroup);
		}
	}

	private void addProbe(Group container) {
		probesCount++;

		Label label = new Label(container, SWT.NONE);
		label.setLayoutData(GridDataFactory.swtDefaults().create());
		label.setText(probesCount + "#");

		Label probeLabel = new Label(container, SWT.NONE);
		probeLabel.setLayoutData(GridDataFactory.swtDefaults().create());
		setLabelAsOk(probeLabel);

		probes.add(probeLabel);
	}

	private void createFanStatusLabel(Group group) {
		Label label = new Label(group, SWT.NONE);
		label.setLayoutData(GridDataFactory.swtDefaults()
				.align(SWT.FILL, SWT.BEGINNING).create());
		label.setText(Messages.FanName);

		fanStatusLabel = new Label(group, SWT.NONE);
		fanStatusLabel.setLayoutData(GridDataFactory.swtDefaults()
				.align(SWT.FILL, SWT.BEGINNING).create());
		setLabelAsOk(fanStatusLabel);
	}

	private void createHeaterStatusLabel(Group group) {
		Label label = new Label(group, SWT.NONE);
		label.setLayoutData(GridDataFactory.swtDefaults().create());
		label.setText(Messages.HeaterName);

		heaterStatusLabel = new Label(group, SWT.NONE);
		heaterStatusLabel.setLayoutData(GridDataFactory.swtDefaults().create());
		setLabelAsOk(heaterStatusLabel);
	}

	private void createACStatusLabel(Group group) {
		Label label = new Label(group, SWT.NONE);
		label.setLayoutData(GridDataFactory.swtDefaults().create());
		label.setText(Messages.ACName);

		acStatusLabel = new Label(group, SWT.NONE);
		acStatusLabel.setLayoutData(GridDataFactory.swtDefaults().create());
		setLabelAsOk(acStatusLabel);
	}

	private final void setLabelAsOk(Label label) {
		label.setText(Messages.LabelOkStatus);
		label.setForeground(label.getDisplay().getSystemColor(
				SWT.COLOR_DARK_GREEN));
		Font font = label.getFont();
		FontData fd[] = font.getFontData();
		for (FontData data : fd) {
			data.setStyle(SWT.BOLD);
		}
		label.setFont(new Font(label.getDisplay(), fd));
	}

	private final void setLabelAsError(Label label) {
		label.setText(Messages.LabelErrorStatus);
		label.setForeground(label.getDisplay().getSystemColor(
				SWT.COLOR_DARK_RED));
		Font font = label.getFont();
		FontData fd[] = font.getFontData();
		for (FontData data : fd) {
			data.setStyle(SWT.BOLD);
		}
		label.setFont(new Font(label.getDisplay(), fd));
	}
	
	public void setProbeStatus(boolean isBroken, int indexInListOfProbes){
		if (isBroken){
			setLabelAsError(probes.get(indexInListOfProbes));
		} else {
			setLabelAsOk(probes.get(indexInListOfProbes));
		}
	}
	
	public void setACStatus(boolean isBroken){
		if (isBroken){
			setLabelAsError(acStatusLabel);
		} else {
			setLabelAsOk(acStatusLabel);
		}
	}

	public void setHeaterStatus(boolean isBroken){
		if (isBroken){
			setLabelAsError(heaterStatusLabel);
		} else {
			setLabelAsOk(heaterStatusLabel);
		}
	}
	
	public void setFanStatus(boolean isBroken){
		if (isBroken){
			setLabelAsError(fanStatusLabel);
		} else {
			setLabelAsOk(fanStatusLabel);
		}
	}
	
	public void setTemperatureMeasurerStatus(boolean isBroken){
		if (isBroken){
			setLabelAsError(tmStatusLabel);
		} else {
			setLabelAsOk(tmStatusLabel);
		}
	}
}
